package apis

import (
	"fmt"
	"log"
	"math"
	"strconv"
	"strings"
)

func dealData(m *map[string]interface{}) {
	var _ccRiskRating map[string]interface{}
	ccRiskRating := (*m)["ccRiskRating"]
	if ccRiskRating != nil {
		_ccRiskRating = ccRiskRating.(map[string]interface{})
	} else {
		_ccRiskRating = make(map[string]interface{}, 0)
	}

	if livingStatus, ok := (*m)["livingStatus"]; ok {
		_m := livingStatus.(map[string]interface{})
		if _m["smokingStatus"] == "正在吸" {
			_m["smokingYearsForGiveUp"] = ""
			_m["smokingYearsForHistory"] = ""
		} else if _m["smokingStatus"] == "已戒烟" {
			_m["smokingYearsForNow"] = ""
			_m["smokingAmountForNow"] = ""
		}
		if _m["isSmoking"] == "是" {
			log.Printf("有吸烟史,设置ccRiskRating")
			_ccRiskRating["fengxianpingji_xiyan"] = "有"
			log.Printf("_ccRiskRating['fengxianpingji_xiyan']设置[有]成功......")
		} else {
			_ccRiskRating["fengxianpingji_xiyan"] = "无"
		}
		if _m["sportStatus"] == "缺乏运动" {
			log.Printf("运动缺乏,设置ccRiskRating")
			_ccRiskRating["fengxianpingji_yundong"] = "是"
			log.Printf("_ccRiskRating['fengxianpingji_yundong']设置[是]成功......")
		} else {
			_ccRiskRating["fengxianpingji_yundong"] = "否"
		}

		(*m)["livingStatus"] = _m
		(*m)["ccRiskRating"] = _ccRiskRating
	}

	if family, ok := (*m)["family"]; ok {
		_m := family.(map[string]interface{})
		if _m["jiazushi_zhongfeng"] == "有" {
			log.Printf("有脑卒中家族史,设置ccRiskRating")
			_ccRiskRating["fengxianpingji_naocuzhong_family_history"] = "有"
			log.Printf("_ccRiskRating['fengxianpingji_naocuzhong_family_history']设置[有]成功......")
		} else {
			_ccRiskRating["fengxianpingji_naocuzhong_family_history"] = "无"
		}
		(*m)["family"] = _m
		(*m)["ccRiskRating"] = _ccRiskRating
	}

	if pastMedicalHistory, ok := (*m)["pastMedicalHistory"]; ok {
		_m := pastMedicalHistory.(map[string]interface{})
		//处理既往病史时候 需要查看体格检查和实验室检查,用以同时确定高血压、血脂、糖尿病
		physicalCheckup := (*m)["physicalCheckup"]
		labExamination := (*m)["labExamination"]
		var _physicalCheckup, _labExamination map[string]interface{}
		if physicalCheckup != nil {
			_physicalCheckup = physicalCheckup.(map[string]interface{})
		} else {
			_physicalCheckup = make(map[string]interface{}, 0)
		}
		if labExamination != nil {
			_labExamination = labExamination.(map[string]interface{})
		} else {
			_labExamination = make(map[string]interface{}, 0)
		}
		dealPastMedicalHistoryUpdateData(_m, _physicalCheckup, _labExamination, _ccRiskRating)
		(*m)["pastMedicalHistory"] = _m
		(*m)["ccRiskRating"] = _ccRiskRating
	}

	if physicalCheckup, ok := (*m)["physicalCheckup"]; ok {
		_m := physicalCheckup.(map[string]interface{})
		//处理体格检查 同时也要查看既往病史
		pastMedicalHistory := (*m)["pastMedicalHistory"]
		var _pastMedicalHistory map[string]interface{}
		if pastMedicalHistory != nil {
			_pastMedicalHistory = pastMedicalHistory.(map[string]interface{})
		} else {
			_pastMedicalHistory = make(map[string]interface{}, 0)
		}

		height, _ := strconv.ParseFloat(_m["tigejiancha_height"].(string), 64)
		weight, _ := strconv.ParseFloat(_m["tigejiancha_weight"].(string), 64)
		bmi := weight / math.Pow(height/100, 2)
		bmi, _ = strconv.ParseFloat(fmt.Sprintf("%.1f", bmi), 64)
		_m["tigejiancha_bmi"] = fmt.Sprint(bmi)
		//体格检查
		//if _m["tigejiancha_bmi"] != nil {
			//_bmi, _ := strconv.ParseFloat(_m["tigejiancha_bmi"].(string), 64)
			if bmi >= 28.0 {
				log.Printf("超重,设置ccRiskRating")
				_ccRiskRating["fengxianpingji_chaozhong"] = "是"
				log.Printf("_ccRiskRating['fengxianpingji_chaozhong']设置[是]成功......")
			} else {
				_ccRiskRating["fengxianpingji_chaozhong"] = "否"
			}
		//}
		var (
			firstSBP, firstDSP, secondSBP, secondDSP int
			gaoxueya                                 string
		)
		if _m["tigejiancha_xueya_firstSBP"] != nil {
			firstSBP, _ = strconv.Atoi(_m["tigejiancha_xueya_firstSBP"].(string))
		}
		if _m["tigejiancha_xueya_firstDSP"] != nil {
			firstDSP, _ = strconv.Atoi(_m["tigejiancha_xueya_firstDSP"].(string))
		}

		if _m["tigejiancha_xueya_secondSBP"] != nil {
			secondSBP, _ = strconv.Atoi(_m["tigejiancha_xueya_secondSBP"].(string))
		}
		if _m["tigejiancha_xueya_secondDSP"] != nil {
			secondDSP, _ = strconv.Atoi(_m["tigejiancha_xueya_secondDSP"].(string))
		}
		if _pastMedicalHistory["gaoxueya"] != nil {
			gaoxueya = _pastMedicalHistory["gaoxueya"].(string)
		}

		if firstSBP >= 140 || secondSBP >= 140 || firstDSP >= 90 || secondDSP >= 90 || gaoxueya == "有" {
			_ccRiskRating["fengxianpingji_gaoxueya"] = "有"
		} else {
			_ccRiskRating["fengxianpingji_gaoxueya"] = "无"
		}
		(*m)["physicalCheckup"] = _m
		(*m)["ccRiskRating"] = _ccRiskRating
	}

	if labExamination, ok := (*m)["labExamination"]; ok {
		_m := labExamination.(map[string]interface{})
		//实验室检查 同时也要查看既往病史
		pastMedicalHistory := (*m)["pastMedicalHistory"]
		var _pastMedicalHistory map[string]interface{}
		if pastMedicalHistory != nil {
			_pastMedicalHistory = pastMedicalHistory.(map[string]interface{})
		} else {
			_pastMedicalHistory = make(map[string]interface{}, 0)
		}
		//实验室检查
		var (
			xuetang_kongfu, xuetang_canhou                        float64
			xuezhi_ganyousanzhi, xuezhi_danguchun, xuezhi_gaomidu float64
			xzyc, tnb                                             string
		)
		if _m["xuetang_kongfu"] != nil {
			xuetang_kongfu, _ = strconv.ParseFloat(_m["xuetang_kongfu"].(string), 64)
		}
		if _m["xuetang_canhou"] != nil {
			xuetang_canhou, _ = strconv.ParseFloat(_m["xuetang_canhou"].(string), 64)
		}

		if _pastMedicalHistory["tnb"] != nil {
			tnb = _pastMedicalHistory["tnb"].(string)
		}
		if xuetang_kongfu >= 7.0 || xuetang_canhou >= 11.0 || tnb == "有" {
			_ccRiskRating["fengxianpingji_tnb"] = "有"
		} else {
			_ccRiskRating["fengxianpingji_tnb"] = "无"
		}

		//----------------------
		if _m["xuezhi_ganyousanzhi"] != nil {
			xuezhi_ganyousanzhi, _ = strconv.ParseFloat(_m["xuezhi_ganyousanzhi"].(string), 64)
		}
		if _m["xuezhi_danguchun"] != nil {
			xuezhi_danguchun, _ = strconv.ParseFloat(_m["xuezhi_danguchun"].(string), 64)
		}
		if _m["xuezhi_gaomidu"] != nil {
			xuezhi_gaomidu, _ = strconv.ParseFloat(_m["xuezhi_gaomidu"].(string), 64)
		}
		if _pastMedicalHistory["xzyc"] != nil {
			xzyc = _pastMedicalHistory["xzyc"].(string)
		}
		if xuezhi_danguchun >= 6.22 || xuezhi_ganyousanzhi >= 2.26 || xuezhi_gaomidu < 1.04 || xzyc == "有" {
			_ccRiskRating["fengxianpingji_xzyc"] = "是"
		} else {
			_ccRiskRating["fengxianpingji_xzyc"] = "否"
		}
		(*m)["labExamination"] = _m
		(*m)["ccRiskRating"] = _ccRiskRating
	}

	ccRiskRatingDeal(_ccRiskRating)
	(*m)["ccRiskRating"] = _ccRiskRating
	return
}

func dealPastMedicalHistoryUpdateData(_pastMedicalHistoryData, _physicalCheckup, _labExamination map[string]interface{}, _ccRiskRating map[string]interface{}) {
	if _pastMedicalHistoryData["gaoxueya"].(string) == "有" || checkGaoXueYaFromPhysicalCheckup(_physicalCheckup) {
		log.Printf("有高血压,设置ccRiskRating")
		_ccRiskRating["fengxianpingji_gaoxueya"] = "有"
		log.Printf("_ccRiskRating['fengxianpingji_gaoxueya']设置[有]成功......")
	} else {
		_ccRiskRating["fengxianpingji_gaoxueya"] = "无"
	}

	if _pastMedicalHistoryData["xzyc"].(string) == "有" || checkXueZhiFromLabExamination(_labExamination) {
		log.Printf("有血脂异常,设置ccRiskRating")
		_ccRiskRating["fengxianpingji_xzyc"] = "是"
		log.Printf("_ccRiskRating['fengxianpingji_xzyc']设置[是]成功......")
	} else {
		_ccRiskRating["fengxianpingji_xzyc"] = "否"
	}
	if _pastMedicalHistoryData["tnb"].(string) == "有" || checkTNBFromLabExamination(_labExamination) {
		log.Printf("有糖尿病,设置ccRiskRating")
		_ccRiskRating["fengxianpingji_tnb"] = "有"
		log.Printf("_ccRiskRating['fengxianpingji_tnb']设置[有]成功......")
	} else {
		_ccRiskRating["fengxianpingji_tnb"] = "无"
	}

	if _pastMedicalHistoryData["xinzangbing"].(string) == "有" && strings.Index(_pastMedicalHistoryData["xinzangbing_leixing"].(string), "房颤") > -1 {
		log.Printf("有心脏病类型是房颤,设置ccRiskRating")
		_ccRiskRating["fengxianpingji_fc"] = "有"
		log.Printf("_ccRiskRating['fengxianpingji_fc']设置[有]成功......")
	} else {
		_ccRiskRating["fengxianpingji_fc"] = "无"
	}
	if _pastMedicalHistoryData["naocuzhong"].(string) == "有" {
		log.Printf("有既往脑卒中,设置ccRiskRating")
		_ccRiskRating["fengxianpingji_naocuzhong_history"] = "有"
		log.Printf("_ccRiskRating['fengxianpingji_naocuzhong_history']设置[有]成功......")
	} else {
		_ccRiskRating["fengxianpingji_naocuzhong_history"] = "无"
	}
	if _pastMedicalHistoryData["naoquexue"].(string) == "有" {
		log.Printf("有既往TIA,设置ccRiskRating")
		_ccRiskRating["fengxianpingji_TIA_history"] = "有"
		log.Printf("_ccRiskRating['fengxianpingji_TIA_history']设置[有]成功......")
	} else {
		_ccRiskRating["fengxianpingji_TIA_history"] = "无"
	}

}

func checkGaoXueYaFromPhysicalCheckup(_m map[string]interface{}) bool {
	var (
		firstSBP, firstDSP, secondSBP, secondDSP int
		b                                        bool
	)
	if _m["tigejiancha_xueya_firstSBP"] != nil {
		firstSBP, _ = strconv.Atoi(_m["tigejiancha_xueya_firstSBP"].(string))
	}
	if _m["tigejiancha_xueya_firstDSP"] != nil {
		firstDSP, _ = strconv.Atoi(_m["tigejiancha_xueya_firstDSP"].(string))
	}

	if _m["tigejiancha_xueya_secondSBP"] != nil {
		secondSBP, _ = strconv.Atoi(_m["tigejiancha_xueya_secondSBP"].(string))
	}
	if _m["tigejiancha_xueya_secondDSP"] != nil {
		secondDSP, _ = strconv.Atoi(_m["tigejiancha_xueya_secondDSP"].(string))
	}
	if firstSBP >= 140 || secondSBP >= 140 || firstDSP >= 90 || secondDSP >= 90 {
		b = true
	} else {
		b = false
	}
	return b
}

func checkXueZhiFromLabExamination(_m map[string]interface{}) bool {
	var b bool
	//实验室检查
	var (
		xuezhi_ganyousanzhi, xuezhi_danguchun, xuezhi_gaomidu float64
	)
	if _m["xuezhi_ganyousanzhi"] != nil {
		xuezhi_ganyousanzhi, _ = strconv.ParseFloat(_m["xuezhi_ganyousanzhi"].(string), 64)
	}
	if _m["xuezhi_danguchun"] != nil {
		xuezhi_danguchun, _ = strconv.ParseFloat(_m["xuezhi_danguchun"].(string), 64)
	}
	if _m["xuezhi_gaomidu"] != nil {
		xuezhi_gaomidu, _ = strconv.ParseFloat(_m["xuezhi_gaomidu"].(string), 64)
	}
	if xuezhi_danguchun >= 6.22 || xuezhi_ganyousanzhi >= 2.26 || (xuezhi_gaomidu < 1.04 && xuezhi_gaomidu > 0) {
		b = true
	} else {
		b = false
	}
	return b
}

func checkTNBFromLabExamination(_m map[string]interface{}) bool {
	var b bool
	//实验室检查
	var (
		xuetang_kongfu, xuetang_canhou float64
	)
	if _m["xuetang_kongfu"] != nil {
		xuetang_kongfu, _ = strconv.ParseFloat(_m["xuetang_kongfu"].(string), 64)
	}
	if _m["xuetang_canhou"] != nil {
		xuetang_canhou, _ = strconv.ParseFloat(_m["xuetang_canhou"].(string), 64)
	}
	if xuetang_kongfu >= 7.0 || xuetang_canhou >= 11.0 {
		b = true
	} else {
		b = false
	}
	return b
}

func ccRiskRatingDeal(ccRiskRatingData map[string]interface{}) {
	var risk_number = 0
	for key, val := range ccRiskRatingData {
		if (key != "fengxianpingji_naocuzhong_history" && key != "fengxianpingji_TIA_history") && (val == "有" || val == "是") {
			risk_number++
		}
	}
	if ccRiskRatingData["fengxianpingji_naocuzhong_history"] == "有" || ccRiskRatingData["fengxianpingji_TIA_history"] == "有" {
		ccRiskRatingData["risk_level"] = "高危"
		ccRiskRatingData["risk_reason"] = "有脑卒中或者TIA"
	} else if risk_number >= 3 {
		ccRiskRatingData["risk_level"] = "高危"
		ccRiskRatingData["risk_reason"] = "患有除脑卒中和TIA之外的其他危险因素项目数量大于等于3"
	} else if risk_number < 3 {
		if ccRiskRatingData["fengxianpingji_gaoxueya"] == "有" || ccRiskRatingData["fengxianpingji_tnb"] == "有" || ccRiskRatingData["fengxianpingji_fc"] == "有" {
			ccRiskRatingData["risk_level"] = "中危"
			ccRiskRatingData["risk_reason"] = "有高血压、糖尿病、房颤或者瓣膜性心脏病三种慢性病之一"
		} else {
			ccRiskRatingData["risk_level"] = "低危"
			ccRiskRatingData["risk_reason"] = "具有三项以下危险因素且无高血压、糖尿病、心房颤动或者瓣膜性心脏病等慢性疾病"
		}
	}
}
